import turtle
import random

# 设置屏幕
screen = turtle.Screen()
screen.title("切西瓜游戏")
screen.bgcolor("lightgreen")
screen.setup(width=800, height=600)
screen.tracer(0)

# 得分
score = 0
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.goto(0, 260)
score_pen.hideturtle()
score_pen.write(f"得分: {score}", align="center", font=("Courier", 24, "normal"))

# 定义西瓜
watermelons = []
for _ in range(5):
    watermelon = turtle.Turtle()
    watermelon.shape("circle")
    watermelon.color("green")
    watermelon.penup()
    x = random.randint(-380, 380)
    y = random.randint(-280, 280)
    watermelon.goto(x, y)
    watermelons.append(watermelon)

# 处理鼠标点击事件
def click(x, y):
    global score
    hit = False
    for watermelon in watermelons:
        if (x - watermelon.xcor()) ** 2 + (y - watermelon.ycor()) ** 2 <= 25 ** 2:
            watermelon.hideturtle()
            x = random.randint(-380, 380)
            y = random.randint(-280, 280)
            watermelon.goto(x, y)
            watermelon.showturtle()
            score += 10
            hit = True
    if not hit:
        score -= 5
    score_pen.clear()
    score_pen.write(f"得分: {score}", align="center", font=("Courier", 24, "normal"))

# 绑定鼠标点击事件
screen.onscreenclick(click)

# 主游戏循环
while True:
    screen.update()